package com.jsmtr.rms.controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jsmtr.rms.entity.*;
import com.jsmtr.rms.utils.StringTools;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.jsmtr.rms.service.IDoGXService;
import com.jsmtr.rms.service.IDoMSService;
import com.jsmtr.rms.service.IProjectService;
import com.jsmtr.rms.service.IUtilService;
import com.jsmtr.rms.utils.EnumTools;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller
@RequestMapping("/Do/GXCampus")
public class DoGXCampusController {

    @Resource
    private IUtilService utilService;

    @Resource
    private IDoGXService doGXService;

    @Resource
    private IProjectService projectService;

    @Resource
    private IDoMSService msService;


    //[start] 简历筛选主界面
    @RequestMapping("/")
    public String Index(HttpServletRequest request, Model model) {
        try {
            model.addAttribute("path", request.getContextPath());
            List<ProjectInfo> projects = projectService.getProjectInfos(EnumTools.RecruitType.高校校园招聘.ordinal());
            StringBuilder pros = new StringBuilder();
            pros.append("<option value=''>直接选择或搜索选择</option>");
            for (ProjectInfo project : projects) {
                if (project.getState() != EnumTools.ProjectState.编辑中.ordinal()) {
                    pros.append("<option value='" + project.getId() + "'>" + project.getPname() + "</option>");
                }
            }

            model.addAttribute("projects", pros);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "Do/GXCampus/index";
    }
    //[end]

    //[start] 获得招聘方案责任部门列表
    @RequestMapping("/getdept")
    public void getdept(HttpServletRequest request, HttpServletResponse response) {
        try {
            String pid = request.getParameter("pid").trim();
            List<ProjectInfoFlow> flows = null;

            BaseUser user = utilService.getSessionUser(request);
            StringBuilder pros = new StringBuilder();

            //当前用户是招聘主管部门
            if (utilService.getConfig("hrcode").equals(user.getUdept())) {
                flows = projectService.getAllFlowDept(pid, 0);
                if(flows!=null&&flows.size()>0){
                    for (ProjectInfoFlow flow : flows) {
                        pros.append("<option value='" + flow.getDeptcode() + "'>" + flow.getDeptname() + "</option>");
                    }
                }
            } else {
               /* flows = projectService.getThisFlowDept(pid, user.getUdept());
                if(flows!=null&&flows.size()>0) {
                    pros.append("<option value='" + flows.get(0).getDeptcode() + "'>" + flows.get(0).getDeptname() + "</option>");
                }*/
                flows = projectService.getAllFlowDept(pid, 0);
                for (ProjectInfoFlow flow : flows) {
                    pros.append("<option value='" + flow.getDeptcode() + "'>" + flow.getDeptname() + "</option>");
                }
            }

            utilService.printTextResult(pros.toString(), response);
        } catch (Exception e) {
            e.printStackTrace();
            utilService.printTextResult("", response);
        }
    }
    //[end]

    //[start] 获得招聘实施流程信息
    @RequestMapping("/getflows")
    public void getFlows(HttpServletRequest request, HttpServletResponse response) {
        try {
            String pid = request.getParameter("pid").trim();
            String deptcode = request.getParameter("deptcode").trim();
            List<ProjectInfoFlow> flows = projectService.getThisFlowDept(pid, deptcode);

            StringBuilder result = new StringBuilder();
            int index = 1;
            for (ProjectInfoFlow flow : flows) {
                int ftype = flow.getFid();
                String url = null;
                if (ftype == EnumTools.GXFlowState.简历筛选.ordinal()) {
                    url = "GXResume/?pid=" + pid + "&fid=" + flow.getId() + "&deptcode=" + flow.getDeptcode();
                } else if (ftype == EnumTools.GXFlowState.面试.ordinal()) {
                    url = "GXInterview/?pid=" + pid + "&fid=" + flow.getId() + "&deptcode=" + flow.getDeptcode();
                } else if (ftype == EnumTools.GXFlowState.背景调查.ordinal()) {
                    url = "GXPReview/?pid=" + pid + "&fid=" + flow.getId() + "&deptcode=" + flow.getDeptcode();
                } else if (ftype == EnumTools.GXFlowState.体检.ordinal()) {
                    url = "GXPhysical/?pid=" + pid + "&fid=" + flow.getId() + "&deptcode=" + flow.getDeptcode();
              /*  } else if (ftype == EnumTools.GXFlowState.岗位分配.ordinal()) {
                    url = "GXPost/?pid=" + pid + "&fid=" + flow.getId() + "&deptcode=" + flow.getDeptcode();*/
                } else if (ftype == EnumTools.GXFlowState.录用.ordinal()) {
                    url = "GXEmploy/?pid=" + pid + "&fid=" + flow.getId() + "&deptcode=" + flow.getDeptcode();
                } else {
                    url = "";
                }
                result.append("<div class='cont cont" + flow.getState() + "'>");
                result.append("<a href='" + request.getContextPath() + "/Do/" + url + "'>");
                result.append("<span class='num'>" + index + "</span>");
                result.append("<span class='txt'>" + flow.getNickname() + "</span>");
                result.append("</a></div>");
                index++;
            }
            result.append("<div class='clear'></div>");
            utilService.printTextResult(result.toString(), response);
        } catch (Exception e) {
            utilService.printTextResult("", response);
        }
    }
    //[end]


    //region  [start] 获得招聘实施流程信息
    @Deprecated
    @RequestMapping(value = "/getFlowsJson_bak",produces = "application/json;charset=utf-8")
    public @ResponseBody
    String getFlowsJson(HttpServletRequest request, HttpServletResponse response) {
        try {

            String pid = request.getParameter("pid").trim();
            String deptcode = request.getParameter("deptcode").trim();
            List<ProjectInfoFlow> flows = projectService.getThisFlowDept(pid, deptcode);
            StringBuilder result = new StringBuilder();
            int index = 1;
            result.append("[");
            for (ProjectInfoFlow flow : flows) {
                int ftype = flow.getFid();
                String url = null;

                if (ftype == EnumTools.GXFlowState.简历筛选.ordinal()) {
                    url = "GXResume/?pid=" + pid + "&fid=" + flow.getId() + "&deptcode=" + flow.getDeptcode();
                    // url = "GXResume/";
                } else if (ftype == EnumTools.GXFlowState.素质.ordinal()) {
                    //  url = "GXInterview/?pid=" + pid + "&fid=" + flow.getId() + "&deptcode=" + flow.getDeptcode();
                    url = "GXQuality/?pid=" + pid + "&fid=" + flow.getId() + "&deptcode=" + flow.getDeptcode();
                    //  url = "GXExam/";
                }else if (ftype == EnumTools.GXFlowState.笔试.ordinal()) {
                  //  url = "GXInterview/?pid=" + pid + "&fid=" + flow.getId() + "&deptcode=" + flow.getDeptcode();
                     url = "GXExam/?pid=" + pid + "&fid=" + flow.getId() + "&deptcode=" + flow.getDeptcode();
                    //  url = "GXExam/";
                } else if (ftype == EnumTools.GXFlowState.面试.ordinal()) {
                    url = "GXInterview/?pid=" + pid + "&fid=" + flow.getId() + "&deptcode=" + flow.getDeptcode();
                    // url = "GXInterview/";
                } else if (ftype == EnumTools.GXFlowState.背景调查.ordinal()) {
                    url = "GXPReview/?pid=" + pid + "&fid=" + flow.getId() + "&deptcode=" + flow.getDeptcode();
                    //   url = "GXPReview/";
                } else if (ftype == EnumTools.GXFlowState.体检.ordinal()) {
                    url = "GXPhysical/?pid=" + pid + "&fid=" + flow.getId() + "&deptcode=" + flow.getDeptcode();
                    //  url = "GXPhysical/";
                 }else if (ftype == EnumTools.GXFlowState.资料审核.ordinal()) {
                   // url = "GXPost/?pid=" + pid + "&fid=" + flow.getId() + "&deptcode=" + flow.getDeptcode();
                    url = "SZCheck/?pid=" + pid + "&fid=" + flow.getId() + "&deptcode=" + flow.getDeptcode();
                    //  url = "SZCheck/";
                }else if (ftype == EnumTools.GXFlowState.录用.ordinal()) {
                    url = "GXEmploy/?pid=" + pid + "&fid=" + flow.getId() + "&deptcode=" + flow.getDeptcode();
                    //  url = "GXEmploy/";
                } else {
                    url = "";
                }
                if(url != ""){
                    String href=request.getContextPath() + "/Do/" + url;
                    result.append("{\"fid\":\""+flow.getId()+"\",\"state\":\""+flow.getState()+"\",\"num\":\""+index+"\",\"txt\":\""+flow.getNickname()+"\",\"url\":\""+href+"\"},");
                    index++;
                }
            }
            result=result.replace(result.lastIndexOf(","),result.length(),"");
            result.append("]");

            //utilService.printTextResult(result.toString(), response);
            return result.toString();
        } catch (Exception e) {
            //utilService.printTextResult("", response);
            return "";
        }

    }
    //region end



    //[start] 公布阶段审核结果（已公布->未公布/未公布->已公布）
    @SuppressWarnings({"rawtypes", "unchecked"})
    @RequestMapping(value = "/PublishResultAction", method = RequestMethod.POST)
    public void PublishResultAction(HttpServletRequest request, HttpServletResponse response) {
        try {
            String[] ids = request.getParameterValues("id");//人员信息ID
            if (ids != null) {
                String fid = request.getParameter("fid").trim();//招聘流程ID
                String deptcode = request.getParameter("deptcode").trim();//责任部门编码
                BaseUser usr = utilService.getSessionUser(request);
                if (usr.getUdept().equals(deptcode)) {
                    //流程是否结束
                    if (utilService.FlowIsDoing(fid)) {
                        List editlist = new ArrayList();
                        List<DUserResume> resumes = doGXService.getResumes(ids);
                        ProjectInfoFlow nFlow = doGXService.getCurrentFlow(fid);
                        String flowName=nFlow.getFname();
                        if (StringTools.trimNotEmpty(flowName)){
                            flowName=nFlow.getNickname();
                        }
                        if (StringTools.trimNotEmpty(flowName)){
                            if (flowName.equalsIgnoreCase(EnumTools.GXFlowState.简历筛选.name())) {
                                for (DUserResume user : resumes) {
                                    if (user.getOp1() == null || user.getOp1().equals("未公布"))
                                        user.setOp1("已公布");
                                    else
                                        user.setOp1("未公布");
                                    editlist.add(user);
                                }
                            }
                            if (flowName.equalsIgnoreCase(EnumTools.GXFlowState.面试.name())) {
                                List<DoMSUser> msUsers = msService.getMsUsers(ids, fid);
                                for (DoMSUser user : msUsers) {
                                    if (user.getOp7() == null || user.getOp7().equals("未公布"))
                                        user.setOp7("已公布");
                                    else
                                        user.setOp7("未公布");
                                    editlist.add(user);
                                }
                            }

                            if (flowName.equalsIgnoreCase(EnumTools.GXFlowState.背景调查.name())) {
                                for (DUserResume user : resumes) {
                                    if (user.getOp3() == null || user.getOp3().equals("未公布"))
                                        user.setOp3("已公布");
                                    else
                                        user.setOp3("未公布");
                                    editlist.add(user);
                                }
                            }
                            if (flowName.equalsIgnoreCase( EnumTools.GXFlowState.体检.name())) {
                                for (DUserResume user : resumes) {
                                    if (user.getOp4() == null || user.getOp4().equals("未公布"))
                                        user.setOp4("已公布");
                                    else
                                        user.setOp4("未公布");
                                    editlist.add(user);
                                }
                            }
                       /* if (nFlow.getFid() == EnumTools.GXFlowState.岗位分配.ordinal()) {
                            for (DUserResume user : resumes) {
                                if (user.getOp6() == null || user.getOp6().equals("未公布"))
                                    user.setOp6("已公布");
                                else
                                    user.setOp6("未公布");
                                editlist.add(user);
                            }
                        }*/
                            if (flowName.equalsIgnoreCase( EnumTools.GXFlowState.签约.name())) {
                                for (DUserResume user : resumes) {
                                    if (user.getOp9() == null || user.getOp9().equals("未公布")) {
                                        user.setOp9("已公布");
                                    }else {
                                        user.setOp9("未公布");
                                    }
                                    editlist.add(user);
                                }
                            }

                            if (flowName.equalsIgnoreCase( EnumTools.GXFlowState.录用.name())) {
                                for (DUserResume user : resumes) {
                                    if (user.getOp5() == null || user.getOp5().equals("未公布"))
                                        user.setOp5("已公布");
                                    else
                                        user.setOp5("未公布");
                                    editlist.add(user);
                                }
                            }

                        }else{
                            if (nFlow.getFid() == EnumTools.GXFlowState.简历筛选.ordinal()) {
                                for (DUserResume user : resumes) {
                                    if (user.getOp1() == null || user.getOp1().equals("未公布"))
                                        user.setOp1("已公布");
                                    else
                                        user.setOp1("未公布");
                                    editlist.add(user);
                                }
                            }
                            if (nFlow.getFid() == EnumTools.GXFlowState.面试.ordinal()) {
                                List<DoMSUser> msUsers = msService.getMsUsers(ids, fid);
                                for (DoMSUser user : msUsers) {
                                    if (user.getOp7() == null || user.getOp7().equals("未公布"))
                                        user.setOp7("已公布");
                                    else
                                        user.setOp7("未公布");
                                    editlist.add(user);
                                }
                            }

                            if (nFlow.getFid() == EnumTools.GXFlowState.背景调查.ordinal()) {
                                for (DUserResume user : resumes) {
                                    if (user.getOp3() == null || user.getOp3().equals("未公布"))
                                        user.setOp3("已公布");
                                    else
                                        user.setOp3("未公布");
                                    editlist.add(user);
                                }
                            }
                            if (nFlow.getFid() == EnumTools.GXFlowState.体检.ordinal()) {
                                for (DUserResume user : resumes) {
                                    if (user.getOp4() == null || user.getOp4().equals("未公布"))
                                        user.setOp4("已公布");
                                    else
                                        user.setOp4("未公布");
                                    editlist.add(user);
                                }
                            }
                       /* if (nFlow.getFid() == EnumTools.GXFlowState.岗位分配.ordinal()) {
                            for (DUserResume user : resumes) {
                                if (user.getOp6() == null || user.getOp6().equals("未公布"))
                                    user.setOp6("已公布");
                                else
                                    user.setOp6("未公布");
                                editlist.add(user);
                            }
                        }*/

                            if (nFlow.getFid() == EnumTools.GXFlowState.录用.ordinal()) {
                                for (DUserResume user : resumes) {
                                    if (user.getOp5() == null || user.getOp5().equals("未公布"))
                                        user.setOp5("已公布");
                                    else
                                        user.setOp5("未公布");
                                    editlist.add(user);
                                }
                            }

                        }



                        if (editlist.size() > 0) {
                            utilService.editList(editlist);
                            utilService.printJsonResult(true, "操作成功！", "no", response);
                        } else {
                            utilService.printJsonResult(false, "当前选中的信息不能执行此操作！", "no", response);
                        }
                    } else {
                        utilService.printJsonResult(false, "当前流程不支持此操作！", "no", response);
                    }
                } else {
                    utilService.printJsonResult(false, "您无权执行此操作！", "no", response);
                }
            } else {
                utilService.printJsonResult(false, "请选择人员信息！", "no", response);
            }
        } catch (Exception e) {
            e.printStackTrace();
            utilService.printJsonResult(false, "操作失败！", "no", response);
        }
    }
    //[end]
}
